﻿#pragma once
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif

enum ConnectCmd_C2S
{
	ConnectCmd_C2S_TcpConnect,
	ConnectCmd_C2S_TcpDisconnect,
	ConnectCmd_C2S_TcpSend,
	ConnectCmd_C2S_TcpList,

	ConnectCmd_C2S_SavedataList,
	ConnectCmd_C2S_SavedataSave,
	ConnectCmd_C2S_SavedataLoad,
	ConnectCmd_C2S_SavedataDelete,

	ConnectCmd_C2S_NodeRegist,
	ConnectCmd_C2S_NodeUnregist,
	ConnectCmd_C2S_NodePublish,
	ConnectCmd_C2S_NodeSubscribe,

	ConnectCmd_C2S_OtaRecived,
	ConnectCmd_C2S_OtaFinshed,
};

enum ConnectCmd_S2C
{
	ConnectCmd_S2C_Wait,

	ConnectCmd_S2C_TcpRecived,
	ConnectCmd_S2C_TcpConnected,
	ConnectCmd_S2C_TcpDisonnected,
	ConnectCmd_S2C_TcpList,

	ConnectCmd_S2C_SavedataList,
	ConnectCmd_S2C_SavedataSave,
	ConnectCmd_S2C_SavedataLoad,
	ConnectCmd_S2C_SavedataDelete,

	ConnectCmd_S2C_NodeRegisted,
	ConnectCmd_S2C_NodeUnregisted,
	ConnectCmd_S2C_NodePublished,
	ConnectCmd_S2C_NodeRecived,

	ConnectCmd_S2C_OtaData,
	ConnectCmd_S2C_OtaFinsh,
};


#define CONNECT_PAYLOAD_START 0xAA55
#define GetUInt24(data) ((data)[0] + (int)((data)[1]) << 8 + (int)((data)[2]) << 16)
#define GetPayloadStart(data) *((const uint16_t*)((const uint8_t*)(data) + 0))
#define GetPayloadSize(data) *((const uint16_t*)((const uint8_t*)(data) + 2))
#define GetPayloadCmd(data) *((const uint8_t*)((const uint8_t*)(data) + 4))
#define GetPayloadHandle(data) GetUInt24((const uint8_t*)(data) + 5)
#define GetPayloadCrc16(data) *((const uint16_t*)((const uint8_t*)(data) + 8))
#define GetPayloadData(data) ((const uint8_t*)(data) + 10)

#define SetUInt24(data, value) {(data)[0] = (value) & 0xFF; (data)[1] = ((value) >> 8) & 0xFF; (data)[2] = ((value) >> 16) & 0xFF;}
#define SetPayloadStart(data) *((uint16_t*)((uint8_t*)(data) + 0)) = CONNECT_PAYLOAD_START
#define SetPayloadSize(data, size) *((uint16_t*)((uint8_t*)(data) + 2)) = (size)
#define SetPayloadCmd(data, cmd) *((uint8_t*)((uint8_t*)(data) + 4)) = (cmd)
#define SetPayloadHandle(data, handle) SetUInt24(((uint8_t*)(data)) + 5, (handle))
#define SetPayloadCrc16(data, crc) *((uint16_t*)((uint8_t*)(data) + 8)) = (crc)
#define SetPayloadData(data, payload) memcpy((uint8_t*)(data) + 10, payload, GetPayloadSize(data))



#ifdef __cplusplus
}
#endif
